package main

import "fmt"

func main() {
	//[2,7,11,15]
	//9
	ret := twoSum([]int{2, 7, 11, 15}, 9)
	fmt.Println(ret)
}

func twoSum(nums []int, target int) []int {
	tmp := map[int]int{}
	l := len(nums)
	for i := 0; i < l; i++ {
		num := target - nums[i]
		if index, ok := tmp[num]; ok {
			return []int{index, i}
		}
		tmp[nums[i]] = i
	}
	return nil
}

func twoSumBak1(nums []int, target int) []int {
	tmp := map[int]int{}
	for i := range nums {
		num := target - nums[i]
		if index, ok := tmp[num]; ok {
			if index > i {
				return []int{i, index}
			}
			return []int{index, i}
		}
		tmp[nums[i]] = i
	}
	return nil
}
